﻿using CampusTrade.Pojo;
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CampusTrade.Dao
{
    public class UserDao : ITransient
    {
        private readonly ISqlSugarRepository repository;
        private readonly SqlSugarClient db;
        public UserDao(ISqlSugarRepository sqlSugarRepository)
        {
            repository = sqlSugarRepository;
            db = repository.Context;
        }

        public long add(User user)
        {
            try
            {
                return db.Insertable(user).ExecuteReturnBigIdentity();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public User selectById(long id)
        {
            try
            {
                return db.Queryable<User>().First(it => it.Id == id);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

        public User selectByOpenId(string openId)
        {
            try
            {
                return db.Queryable<User>().First(it => it.OpenId == openId);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }
        public int update(User user)
        {
            try
            {
                return db.Updateable(user).ExecuteCommand();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }

        public int delete(long id)
        {
            try
            {
                return db.Deleteable<User>().Where(it => it.Id == id).ExecuteCommand();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return -1;
            }
        }
    }
}
